#!/bin/bash


if [ "$#" -lt 1 ];then
    echo "usage: `basename $0` [version]"
    exit 1
fi

which repoquery >& /dev/null || { echo "repoquery must be installed locally"; exit 1; }

naturalSort=0

while getopts ":n" opt; do
  case ${opt} in
    n ) # process option t
	  naturalSort=1
      ;;
    \? ) echo "Usage: cmd [-n]"
      ;;
  esac
done
shift $((OPTIND -1))

version=$1

minor_ver=`echo ${version} | cut -d '.' -f1,2`
micro_ver=`echo ${version} | cut -d '.' -f3`

if [[ -z $micro_ver ]];then
    micro_ver=0
fi

logfile=pkg-ohpc
rm -f ${logfile}.*

options=""
skip=""

baseos=""
arch=""



echo $version | egrep -q "^1.0"
if [ $? -eq 0 ];then
    pwd | grep -q centos7 && baseos="CentOS_7.1"
fi

echo $version | egrep -q "^1.2|^1.1"
if [ $? -eq 0 ];then
    pwd | grep -q sles12  && baseos="SLE_12_SP1"
    pwd | grep -q centos7 && baseos="CentOS_7.2"
fi

echo $version | egrep -q "^1.3"
if [ $? -eq 0 ];then
    pwd | grep -q sles12  && baseos="SLE_12"
    pwd | grep -q centos7 && baseos="CentOS_7"
fi

pwd | grep -q aarch64 && arch="aarch64"
pwd | grep -q x86_64  && arch="x86_64"
    
if [[ -z ${baseos} ]];then
    echo "Unknown or unsupported base os"
    exit 1;
fi

if [[ -z ${arch} ]];then
    echo "Unknown or unsupported architecture"
    exit 1;
fi

echo "Querying available packages for baseOS=${baseos}, arch=${arch}: minor_ver=${minor_ver}, micro_ver=${micro_ver}"

if [[ "${arch}" == "aarch64" ]];then
    echo "Querying aarch64..."
    skip="mvapich2|impi-ohpc|lmod-defaults-intel"
fi

repobase="http://build.openhpc.community/OpenHPC:/${minor_ver}/${baseos}"
if [[ $micro_ver -gt 0 ]];then
    repoupdate="http://build.openhpc.community/OpenHPC:/${minor_ver}:/Update${micro_ver}/${baseos}"
fi

# include noarch in query
arch="$arch,noarch"

# pick a sort pony
export LC_COLLATE=C

# query repo for ohpc packages
if [[ $micro_ver -eq 0 ]];then
    if [ -z "$skip" ];then
	repoquery --archlist=${arch} --repofrompath="ohpc-base,${repobase}" --repoid=ohpc-base -a \
	    --queryformat='%{Name} %{Version} %{URL} %{Group} %{Summary}\n' | grep -e "-ohpc\b" | sort >> ${logfile}.all
    else
	repoquery --archlist=${arch} --repofrompath="ohpc-base,${repobase}" --repoid=ohpc-base -a \
	    --queryformat='%{Name} %{Version} %{URL} %{Group} %{Summary}\n' | grep -e "-ohpc\b" | egrep -v  $skip | sort >> ${logfile}.all
    fi

    # query meta-packages
    repoquery --archlist=${arch} --repofrompath="ohpc-base,${repobase}" --repoid=ohpc-base \
	--queryformat='%{Name} %{Group} %{Description}\n' ohpc* | grep  ohpc/meta-package | cut -d' ' -f1,3- | grep -e "^ohpc-"| sort > pattern-ohpc.all

else
    if [ -z "$skip" ];then
	repoquery --archlist=${arch} --repofrompath="ohpc-base,${repobase}" --repoid=ohpc-base -a \
	    --repofrompath="ohpc-update,${repoupdate}" --repoid=ohpc-update \
	    --queryformat='%{Name} %{Version} %{URL} %{Group} %{Summary}\n' | grep -e "-ohpc\b" | sort  >> ${logfile}.all
    else
	repoquery --archlist=${arch} --repofrompath="ohpc-base,${repobase}" --repoid=ohpc-base -a \
	    --repofrompath="ohpc-update,${repoupdate}" --repoid=ohpc-update \
	    --queryformat='%{Name} %{Version} %{URL} %{Group} %{Summary}\n' | grep -e "-ohpc\b" | egrep -v  $skip | sort >> ${logfile}.all
    fi

    # query meta-packages
    repoquery --archlist=${arch} --repofrompath="ohpc-base,${repobase}" --repoid=ohpc-base \
	--repofrompath="ohpc-update,${repoupdate}" --repoid=ohpc-update \
	--queryformat='%{Name} %{Group} %{Description}\n' ohpc* | grep  ohpc/meta-package | cut -d' ' -f1,3- | grep -e "^ohpc-"| sort > pattern-ohpc.all
fi

# include ohpc-release
if [[ $micro_ver -eq 0 ]];then
    info=`repoquery --archlist=${arch} --repofrompath="ohpc-base,${repobase}" --repoid=ohpc-base -q \
--queryformat='%{Name} %{Version} %{URL} %{Group} %{Summary}\n' ohpc-release`
else
    info=`repoquery --archlist=${arch} --repofrompath="ohpc-base,${repobase}" --repoid=ohpc-base -q \
--repofrompath="ohpc-update,${repoupdate}" --repoid=ohpc-update \
--queryformat='%{Name} %{Version} %{URL} %{Group} %{Summary}\n' ohpc-release`
fi
echo ${info} >>${logfile}.all

echo
echo ++++ list of OpenHPC package groups
groups=`grep -o '\sohpc/[^ ]*' pkg-ohpc.all | sort -u | cut -d '/' -f 2`

echo $groups
echo

for grp in ${groups}; do
    if [[ $naturalSort -eq 1 ]];then
	grep "ohpc/${grp}" ${logfile}.all | sort   >& ${logfile}.${grp} 
    else
	grep "ohpc/${grp}" ${logfile}.all | sort -k 1.1,1.2 -k2V  >& ${logfile}.${grp} 
    fi

done

